import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
// 导入组件
const routes = [
  // 登录注册
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register.vue'),
  },
  // 跳转测试页
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/home.vue'),
  },
  // 教师端路由
  {
    path: '/teacher/dashboard',
    name: 'TeacherDashboard',
    component: () => import('../views/teacher/dashboard/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'teacher',
      title: '教师仪表盘'
    }
  },
  {
    path: '/ai',
    name: 'ai_conversion',
    component: () => import('../views/ai/index.vue'),
    meta: {
      requiresAuth: true,
      title: 'AI助手'
    }
  },
  {
    path: '/teacher/assignment/assign_homework',
    name: 'assign_homework',
    component: () => import('../views/teacher/assignment/assign_homework.vue'),
    meta: {
      requiresAuth: true,
      title: '布置作业'
    }
  },
  {
    path: '/teacher/assignment/see_homework/:homeworkId',
    name: 'see_homework',
    component: () => import('../views/teacher/assignment/see_homework.vue'),
    meta: {
      requiresAuth: true,
      title: '查看作业'
    }
  },
  {
    path: '/teacher/assignment/correct_homework',
    name:'correct_homework',
    component: () => import('../views/teacher/assignment/correct_homework.vue'),
    meta: {
      requiresAuth: true,
      title: '批改作业'
    }
  },
  {
    path: '/teacher/assignment/:id',
    name: 'TeacherAssignment',
    component: () => import('../views/teacher/assignment/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '作业详情'
    }
  },
  {
    path: '/teacher/feedback/card',
    name: 'TeacherFeedback',
    component: () => import('../views/teacher/feedback/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '质量反馈'
    }
  },
  {
    path: '/teacher/feedback/result/:surveyId',
    name: 'FeedbackResult',
    component: () => import('@/views/teacher/feedback/result/index.vue'),
    meta: {
      title: '反馈结果',
      requiresAuth: true
    }
  },
  {
    path: '/teacher/signin/teacher',
    name: 'signin',
    component: () => import('../views/teacher/signin/teacher.vue'),
    meta: {
      requiresAuth: true,
      title: '签到'
    }
  },
  {
    path: '/teacher/chat/index',
    name:'AIGCconversion',
    component: () => import('../views/teacher/chat/index.vue'),
    meta: {
      requiresAuth: true,
      title: 'AIGC智能聊天室'
    }
  },
  {
    path: '/teacher/feedback/:id',
    name: 'TeacherFeedback',
    component: () => import('../views/teacher/feedback/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '质量反馈卡片'
    }
  },
  {
    path: '/teacher/assignment/card/:homeworkId?',
    name: 'TeacherAssignmentCard',
    component: () => import('../views/teacher/assignment/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '教师作业卡片'
    }
  },
  {
    path: '/teacher/record/card',
    name: 'TeacherRecordCard',
    component: () => import('../views/teacher/record/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '教师记录卡片'
    }
  },
  {
    path: '/teacher/manager',
    name: 'TeacherManager',
    component: () => import('../views/teacher/manager/index.vue'),
    meta: {
      requiresAuth: true,
      title: '教师管理'
    } 
  },
  {
    path: '/teacher/manager/course/details/:courseManageId',
    name: 'CourseDetails',
    component: () => import('@/views/teacher/manager/course/details.vue'),
    props: true,
    meta: {
      requiresAuth: true,
      title: '课程详情'
    }
  },
  {
    path: '/teacher/manager/lessonschedule/:courseManageId',
    name: 'TeacherManagerCourseLessonSchedule',
    component: () => import('../views/teacher/manager/course/lessonschedule.vue'),
    props: true,
    meta: {
      requiresAuth: true,
      title: '课程教学计划'
    }
  },
  {
    path: '/teacher/manager/student',
    name: 'TeacherManagerStudent',
    component: () => import('../views/teacher/manager/course/studentManager.vue'),
    meta: {
      requiresAuth: true,
      title: '学生管理'
    } 
  },
  {
    path: '/teacher/manager/interaction',
    name: 'TeacherManagerInteraction',
    component: () => import('../views/teacher/manager/course/interaction.vue'),
    meta: {
      requiresAuth: true,
      title: '互动管理'
    } 
  }, 
  {
    path: '/teacher/manager/interaction2',
    name: 'TeacherManagerInteraction2',
    component: () => import('../views/teacher/manager/course/interaction2.vue'),
    meta: {
      requiresAuth: true,
      title: '数据统计'
    } 
  },
  {
    path: '/teacher/manager/dialogue',
    name: 'TeacherManagerDialogue',
    component: () => import('../views/teacher/manager/course/dialogue.vue'),
    meta: {
      requiresAuth: true,
      title: '对话管理'
    } 
  },
  {
    path: '/teacher/manager/class/:id',
    name: 'TeacherManagerClass',
    component: () => import('../views/teacher/manager/course/class.vue'),
    meta: {
      requiresAuth: true,
      title: '课时管理'
    } 
  },
  //学生端路由
  {
    path: '/student/dashboard',
    name: 'StudentDashboard',
    component: () => import('../views/student/dashboard/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '学生仪表盘'
    }
  },
  {
    path: '/student/homework/card',
    name: 'StudentHomeworkCard',
    component: () => import('../views/student/homework/card/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '作业卡片'
    }
  },
  {
    path: '/student/homework/submit/submit',
    name: 'SubmitHomework',
    component: () => import('../views/student/homework/submit/submit.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '提交作业'
    }
  },
  {
    path: '/student/feedback/card',
    name: 'StudentFeedbackCard',
    component: () => import('../views/student/feedback/card/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '课堂质量实时反馈'
    }
  },
  {
    path: '/student/mycourse',
    name: 'mycourse',
    component: () => import('@/views/student/mycourse/index.vue'),
    children: [
      {
        path: 'details/:id',
        name: 'courseDetails',
        component: () => import('@/views/student/mycourse/details.vue')
      }
    ]
  },
  {
    path: '/student/homework/see_correcthomework',
    name: 'SeeCorrectHomework',
    component: () => import('../views/student/homework/see_correcthomework.vue'),
    meta: { 
      requiresAuth: true,
      role: 'student'
    }
  },
  {
    path: '/student/signin/index',
    name: 'student_signin',
    component: () => import('../views/student/signin/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '签到'
    }
  },
  {
    path: '/student/suggestion/index',
    name: 'student_suggestion',
    component: () => import('../views/student/suggestion/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '意见反馈问卷'
    }
  },
  {
    path: '/student/chat',
    name: 'student_chat',
    component: () => import('../views/student/chat.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '聊天室'
    },
  },
  {
    path: '/teacher/information',
    name: 'teacher_information',
    component: () => import('../views/teacher/information/personal-information.vue'),
    meta: {
      requiresAuth: true,
      role: 'teacher',
      title: '个人信息'
    },
  },
  {
    path: '/teacher/information/ss',
    name: 'teacher_informationss',
    component: () => import('../views/teacher/information/ss.vue'),
    meta: {
      requiresAuth: true,
      role: 'teacher',
      title: '备用个人信息'
    },
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: () => import('@/views/login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/teacher/course/student-manager',
      name: 'StudentManager',
      component: () => import('@/views/teacher/manager/course/studentManager.vue'),
      meta: { requiresAuth: true }
    },
    ...routes
  ]
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  
  if (to.meta.requiresAuth && !token) {
    ElMessage.warning('请先登录');
    next('/login');
  } else {
    next();
  }
});

export default router;